package com.nianzhen.utils;


import android.content.Context;

import com.lidroid.xutils.util.LogUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

public class CHSHAsyncHttpClient {
	private static AsyncHttpClient client = null;
	private static PersistentCookieStore cookieStore = new PersistentCookieStore(
			DateLib.getAppContext());
	//private static PersistentCookieStore pCookieStore = null;
	private static AsyncHttpClient getMyAsyncHttpClient(Context context) {
		if (client == null)
			init(context);

		return client;
	}
	protected synchronized static void init(final Context ctx) {
		if (client != null)
			return;
		//pCookieStore = new PersistentCookieStore(ctx);
		client = new AsyncHttpClient();
		client.setTimeout(10 * 1000);
		client.setConnectTimeout(10 * 1000);
		client.setResponseTimeout(10 * 1000);
		client.setCookieStore(cookieStore);
	}
	
	
	
	
	public static RequestHandle get(Context context, String url, RequestParams params,
			JsonHttpResponseHandler responseHandler) {
		return getMyAsyncHttpClient(context).get(context, url, params, responseHandler);
	}

	
	public static RequestHandle post(Context context, String url, RequestParams params,
			JsonHttpResponseHandler responseHandler) {
		LogUtils.d(url);
		LogUtils.d(params.toString());
		return getMyAsyncHttpClient(context).post(context, url, params, responseHandler);
	}
	
	

	
	/**
	 * This should be invoked in the application start time.
	 * 
	 * @param ctx
	
	protected synchronized static void init(final Context ctx) {
		if (client != null)
			return;

		//pCookieStore = new PersistentCookieStore(ctx);
		client = new AsyncHttpClient();
		client.setTimeout(10 * 1000);
		client.addHeader("app", PackageHelper.getPackageName(ctx) + ":" + PackageHelper.getChannel(ctx));
		client.addHeader("channel", PackageHelper.getChannel(ctx));
		client.addHeader("pkg", PackageHelper.getPackageName(ctx));
		client.addHeader("ver", PackageHelper.getVersion(ctx));
		client.addHeader("Referer", "http://www.tifen.com");
		//client.setCookieStore(pCookieStore);
	}

	private static AsyncHttpClient getMyAsyncHttpClient(Context context) {
		if (client == null)
			init(context);

		return client;
	}

	public static void deleteAllCookie() {
		if (pCookieStore != null)
			pCookieStore.clear();
	}

	public static RequestHandle get(Context context, String url, RequestParams params,
			AsyncHttpResponseHandler responseHandler) {
		return getMyAsyncHttpClient(context).get(context, url, params, responseHandler);
	}

	public static RequestHandle post(Context context, String url, HttpEntity entity, String contentType,
			AsyncHttpResponseHandler responseHandler) {
		return getMyAsyncHttpClient(context).post(context, url, entity, contentType, responseHandler);
	}

	public static RequestHandle post(Context context, String url, RequestParams params,
			AsyncHttpResponseHandler responseHandler) {
		return getMyAsyncHttpClient(context).post(context, url, params, responseHandler);
	}

	public static RequestHandle postWithHeader(Context context, String url, Header[] headers, HttpEntity entity,
			String contentType, AsyncHttpResponseHandler responseHandler) {
		return getMyAsyncHttpClient(context).post(context, url, headers, entity, contentType, responseHandler);
	}

	public static List<Cookie> getCookie() {
		if (pCookieStore == null)
			init(TifenLib.getAppContext());
		return pCookieStore.getCookies();
	}

	public static void clearCookie(Context context) {
		PersistentCookieStore pCookieStore = new PersistentCookieStore(context);
		pCookieStore.clear();
		getMyAsyncHttpClient(context).setCookieStore(pCookieStore);
	}

	public static void setCookie(Context context, CookieStore cs) {
		PersistentCookieStore pCookieStore = new PersistentCookieStore(context);
		for (Cookie c : cs.getCookies()) {
			pCookieStore.addCookie(c);
		}
	}
	 */
}
